<?php
namespace app\admin\service;

use app\admin\model\Role;

/**
 * 权限相关业务
 * @ date: 2019年11月5日 下午5:09:04
 * @ author: 月光下的魔术师
 */
class Permissions
{

    protected $path;

    protected $role;

    public function __construct()
    {
        $this->role = new Role();
        $this->path = dirname($_SERVER['SCRIPT_FILENAME']) . '/permissions.json';
    }

    public function getMenu($rId)
    {
        $menu = file_get_contents($this->path);
        $menu = json_decode($menu, true);
        if (empty($rId)) {
            return $menu;
        }
        $role = $this->role->field('id,name,rules,info')->find($rId);
        $rules = explode(',', $role['rules']);
        $r = [];
        foreach ($menu as $k => $v) {
            if (in_array($v['id'], $rules)) {
                $t = $v;
                $t['children'] = [];
                foreach ($v['children'] as $k1 => $v1) {
                    if (in_array($v1['id'], $rules)) {
                        $t['children'][] = $v1;
                    }
                }
                $r[] = $t;
            }
        }
        return $r;
    }
}
?>